package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-04-30
 * Time: 16:57
 */
/*
我的帖子页测试
 */
public class MyPostsPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public MyPostsPage() {
        super(url);
    }

    // 进入我的帖子测试
    public void MyPostEter() {
        // 需要重新登录
        // 返回登录页并刷新页面
        driver.navigate().refresh();
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 点击 "用户头像" 按钮, 进入选择进入用户我的帖子界面
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();
        // 点击 "我的帖子" 按钮, 进入个人中心
        driver.findElement(By.cssSelector("#index_user_profile")).click();
        // 获取界面元素
        WebElement ele1 = driver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate"));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele1));
        String ele2 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-4 > div > div > div > div > h2")).getText();
        // 断言判断是否成功进入
        assert ele2.equals("个人介绍");
    }

    // 点击帖子进入帖子详情页
    // 因为帖子是动态变化的, 无法选中标题从而进入, 人为测试正常, 自动化测试脚本跳过
    public void PostEnterSuc() {
        // 选择一篇帖子标题, 点击进入
        driver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate")).click();
        // 获取页面元素
        WebElement ele3 = driver.findElement(By.cssSelector("#details_btn_like_count"));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1));
        wait.until(ExpectedConditions.elementToBeClickable(ele3));
        // 断言判断是否进入
        assert ele3.equals("点赞");
    }
}
